class ReceptionsController < ApplicationController
  # GET /receptions
  # GET /receptions.xml
  def index
    #@receptions = Reception.find(:all, :order => 'created_at desc')
    @receptions = Reception.paginate(:page => params[:page], :per_page => 10 , :order => 'created_at desc')
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @receptions }
    end
  end
  
  # GET /receptions/1
  # GET /receptions/1.xml
  def show
    @reception = Reception.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @reception }
    end
  end
  
  # GET /receptions/new
  # GET /receptions/new.xml
  def new
    @reception = Reception.new
    @members = Member.find(:all)
    @default_member_id = cookies[:default_member_id]
    respond_to do |format|
      if @members.nil? || @members.empty?
        flash[:notice] = 'メンバーを先に登録してください。'
        format.html { redirect_to(:controller => 'members', :action => 'new') }
      else
        format.html # new.html.erb
        format.xml  { render :xml => @reception }
      end
    end
  end
  
  # GET /receptions/1/edit
  def edit
    @reception = Reception.find(params[:id])
    @members = Member.find(:all)
  end
  
  # POST /receptions
  # POST /receptions.xml
  def create
    @reception = Reception.new(params[:reception])
    
    respond_to do |format|
      if @reception.save
        cookies[:default_member_id] = @reception.member_id.to_s
        flash[:notice] = 'Reception was successfully created.'
        #format.html { redirect_to(@reception) }
        format.html { redirect_to(:action => 'index') }
        format.xml  { render :xml => @reception, :status => :created, :location => @reception }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @reception.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # PUT /receptions/1
  # PUT /receptions/1.xml
  def update
    @reception = Reception.find(params[:id])
    
    respond_to do |format|
      if @reception.update_attributes(params[:reception])
        flash[:notice] = 'Reception was successfully updated.'
        format.html { redirect_to(@reception) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @reception.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /receptions/1
  # DELETE /receptions/1.xml
  def destroy
    @reception = Reception.find(params[:id])
    @reception.destroy
    
    respond_to do |format|
      format.html { redirect_to(receptions_url) }
      format.xml  { head :ok }
    end
  end
end
